package com.ym.pms.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.pms.model.Contract;
import com.ym.pms.service.ContractServices;
import com.ym.pms.service.IFileUploadService;
import com.ym.pms.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author lmt
 * @since 2020-12-23
 */
@Api(tags = "合同接口")
@RestController
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    private ContractServices contractService;
    @Autowired
    private IFileUploadService fileUploadService;

    @Value("${fileUpload.dirPath}")
    private String dirPath;

    @ApiOperation(value = "生成合同")
    @PostMapping("/create")
    public JSONResult createInfo(@ApiParam("合同表") @RequestBody Contract contract){
        contractService.addInitInfo(contract);
        return JSONResult.ok();
    }
    @ApiOperation(value = "签字照片上传")
    @PostMapping("/photo/{id}")
    public JSONResult createInfo(@ApiParam("合同id") @PathVariable Integer id,MultipartFile upload) throws IOException {
        String path = dirPath;
        path = fileUploadService.fileUploadInServer(upload, path);
        contractService.addPhoto(id,path);
        return JSONResult.ok(path);
    }

    @ApiOperation(value = "乙方签名")
    @PutMapping("/save/{id}")
    public JSONResult save(@ApiParam("合同id") @PathVariable Integer id,@ApiParam("乙方姓名") String coPBName, @ApiParam("乙方电话") String coPBPNo){
        contractService.saveInfo(id,coPBName,coPBPNo);
        return JSONResult.ok();
    }
    @ApiOperation(value ="显示所有合同" )
    @GetMapping("/queryAll/{page}/{size}")
    public JSONResult queryAll(@PathVariable Integer page,@PathVariable Integer size){
        PageHelper.startPage(page,size);
        List<Contract> contractList= contractService.queryAll();
        PageInfo<Contract> pageInfo = new PageInfo<>(contractList);
        return JSONResult.ok(pageInfo);
    }
    @ApiOperation(value = "查看合同详情")
    @GetMapping("/queryOne/{id}")
    public JSONResult findOne(@PathVariable Integer id){
        Contract contract=contractService.queryOne(id);
        return JSONResult.ok(contract);
    }
    @ApiOperation(value = "根据条件查找合同")
    @PostMapping("/search/{page}/{size}")
    public JSONResult searchInfo(@RequestBody Contract contract,@PathVariable Integer page,@PathVariable Integer size){
        PageHelper.startPage(page,size);
        List<Contract> contractList= contractService.search(contract);
        PageInfo<Contract> pageInfo = new PageInfo<>(contractList);
        return JSONResult.ok(pageInfo);
    }
    @ApiOperation(value = "修改合同状态",notes = "违约情况，强制终止")
    @PutMapping("/update/{id}")
    public JSONResult updateInfo(@PathVariable Integer id,@ApiParam("合同终止原因") String coRFT){
        contractService.updateInfo(id,coRFT);
        return JSONResult.ok();
    }
    @ApiOperation(value = "删除合同")
    @DeleteMapping("/delete/{id}")
    public JSONResult deleteInfo(@PathVariable Integer id){
        contractService.deleteInfo(id);
        return JSONResult.ok();
    }
}

